import { ref } from "vue";
import _ from "lodash";

export default function useImgScaleTap() {
  let isFullScreen = ref(false);
  let startXY = ref({
    x: 0,
    y: 0,
  });

  let endXY = ref({
    x: 0,
    y: 0,
  });

  let touchstart = (evt: any) => {
    startXY.value = {
      x: _.get(evt, "changedTouches[0].clientX"),
      y: _.get(evt, "changedTouches[0].clientY"),
    };
  };

  let touchend = (evt: any) => {
    endXY.value = {
      x: _.get(evt, "changedTouches[0].clientX"),
      y: _.get(evt, "changedTouches[0].clientY"),
    };
    let sqrt =
      Math.pow(endXY.value.x - startXY.value.x, 2) +
      Math.pow(endXY.value.y - startXY.value.y, 2);
    let distance = Math.sqrt(sqrt);
    if (distance <= 0) {
      // uniapp 报错hack
      setTimeout(() => {
        isFullScreen.value = !isFullScreen.value;
      });
    }
  };

  let current = ref(0);
  let handleCurrentChange = (evt: any) => {
    current.value = _.get(evt, "detail.current");
  };

  let toFullScreen = () => {
    isFullScreen.value = true;
  };

  return {
    isFullScreen,
    touchstart,
    touchend,
    current,
    handleCurrentChange,
    toFullScreen,
  };
}
